package com.changgou.goods.service.impl;

import com.changgou.goods.dao.BrandMapper;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.changgou.goods.vo.BrandVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    public BrandMapper brandMapper;

    /**
     * 查询全部
     * @return
     */
    @Override
    public List<Brand> findAll() {
        List<Brand> brands = brandMapper.selectAll();
        return brands;
    }

    /**
     * 根据id查询
     * @return
     */
    @Override
    public Brand findById(int id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        return brand;
    }

    /**
     * 分页查询
     */
    @Override
    public Page<Brand> findByPage(int page, int size) {
        Page<Brand> page1 = PageHelper.startPage(page, size);
        return (Page<Brand>) brandMapper.selectAll();
    }

    /**
     * 添加
     */
    @Override
    public void save(Brand brand) {
        brandMapper.insertSelective(brand);
    }

    /**
     * 修改
     */
    @Override
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKey(brand);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(int id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    @Override
    final public List<Brand> findByCondition(Map<String, Object> map) {
        System.out.println("BrandServiceImpl >> findByCondition");
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();

        if (map != null) {
            //品牌名称
            if (map.get("name") != null && !"".equals(map.get("name"))) {
                criteria.andLike("name", "%" + map.get("name") + "%");
            }
            //关键字
            if (map.get("letter") != null && !"".equals(map.get("letter"))) {
                criteria.andLike("letter","%"+map.get("letter")+"%");
            }
        }
        List<Brand> brands = brandMapper.selectByExample(example);
        return brands;
    }

    /**
     * 条件分页查询
     * @param map
     * @param page
     * @param size
     * @return
     */

    @Override
    public Page<Brand> findByPageCondition(Map<String, Object> map, int page, int size) {
        System.out.println("BrandServiceImpl >> findByPageCondition  22 22");
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if (map != null) {
            //品牌名称
            if (map.get("name") != null && !"".equals(map.get("name"))) {
                criteria.andLike("name", "%" + map.get("name") + "%");
            }
            //关键字
            if (map.get("letter") != null && !"".equals(map.get("letter"))) {
                criteria.andLike("letter","%"+map.get("letter")+"%");
            }
        }
        Page<Brand> page1 = PageHelper.startPage(page, size);
        List<Brand> brands = brandMapper.selectByExample(example);
        return (Page<Brand>) brands;
    }
    /**
     * 根据商品分类名称查询品牌列表
     */
    @Override
    public List<BrandVo> findBrandNameByCategoryName(String categoryName) {
        List<BrandVo> brandNameByCategoryName = brandMapper.findBrandNameByCategoryName(categoryName);
        return brandNameByCategoryName;
    }
}
